package system

import (
	"context"
	"github.com/samber/lo"
	"private-domain-overseas-service/admin/business/api/internal/repository/system"
	"private-domain-overseas-service/tools/ent"
	"private-domain-overseas-service/tools/ent/hzsystemappversion"
	"private-domain-overseas-service/tools/ent/predicate"
	"time"

	"private-domain-overseas-service/admin/business/api/internal/svc"
	"private-domain-overseas-service/admin/business/api/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type SystemVersionListLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

// NewSystemVersionListLogic - 版本信息分页列表
func NewSystemVersionListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *SystemVersionListLogic {
	return &SystemVersionListLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *SystemVersionListLogic) SystemVersionList(req *types.SystemVersionListRequest) (resp *types.SystemVersionListResponse, err error) {
	// 创建实例
	versionDao := system.NewSystemVersionDao(l.ctx, l.svcCtx)

	// 查询版本信息
	versionConditions := make([]predicate.HzSystemAppVersion, 0)
	versionConditions = append(versionConditions, hzsystemappversion.DeleteAtIsNil())
	if req.Id != 0 {
		versionConditions = append(versionConditions, hzsystemappversion.IDEQ(int(req.Id)))
	}
	if req.AppPlatform != 0 {
		versionConditions = append(versionConditions, hzsystemappversion.AppPlatformEQ(req.AppPlatform))
	}
	if req.AppPublishStatus != 0 {
		versionConditions = append(versionConditions, hzsystemappversion.AppPublishStatusEQ(req.AppPublishStatus))
	}
	if len(req.Keyword) > 0 {
		versionConditions = append(versionConditions, hzsystemappversion.Or(
			hzsystemappversion.AppVersionDescription(req.Keyword),
		))
	}
	versionList, verCount, err := versionDao.SystemVersionConditionsAllByPage(versionConditions, req.Current, req.PageSize)
	if err != nil {
		return nil, err
	}

	// 组装数据
	list := lo.Map(versionList, func(item *ent.HzSystemAppVersion, _ int) *types.SystemVersionDetail {
		return &types.SystemVersionDetail{
			SystemVersionInfo: types.SystemVersionInfo{
				AppPackageName:        item.AppPackageName,
				AppVersion:            item.AppVersion,
				AppPlatform:           item.AppPlatform,
				AppVersionTitle:       item.AppVersionTitle,
				AppForceUpdateType:    item.AppForceUpdateType,
				AppHotUpdateType:      item.AppHotUpdateType,
				AppPublishStatus:      item.AppPublishStatus,
				AppSdkUpdateType:      item.AppSdkUpdateType,
				AppUpdateDownloadURL:  item.AppUpdateDownloadURL,
				AppVersionDescription: item.AppVersionDescription,
			},
			Id:        int64(item.ID),
			CreatedAt: time.Unix(item.CreatedAt, 0).Format(time.DateTime),
			UpdatedAt: time.Unix(item.UpdateAt, 0).Format(time.DateTime),
		}
	})

	// 返回数据
	resp = &types.SystemVersionListResponse{
		PageResponse: types.PageResponse{
			Total:           verCount,
			PageSize:        int64(len(list)),
			RequestPageSize: req.PageSize,
			Current:         req.Current,
		},
		List: list,
	}

	return
}
